//
//  ThemeSelectController.m
//  Project_2_weibo
//
//  Created by 林林 on 16/6/17.
//  Copyright © 2016年 lhn. All rights reserved.
//

#import "ThemeSelectController.h"

@interface ThemeSelectController () <UITableViewDelegate, UITableViewDataSource>
{
    NSDictionary *_themeDic;
    NSInteger _selectRow;
    UIColor *_textColor;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ThemeSelectController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _selectRow = -1;
    _textColor = [[ThemeManager sharedManager] themeColorWithColorName:@"More_Item_Text_color"];
    self.title = @"主题选择";
    //注册单元格
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //加载数据
    _themeDic = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UItable DataSource 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _themeDic.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    //数据
    NSArray *themeNames = _themeDic.allKeys;
    NSString *themeKey = themeNames[indexPath.row];
    NSString *themePath = _themeDic[themeKey];
    
    //拼接图片路径
    NSString *iconPath = [NSString stringWithFormat:@"%@/%@", themePath, @"more_icon_theme.png"];
    //获取图标
    UIImage *iconImage = [UIImage imageNamed:iconPath];
    
    //设置文本颜色
    cell.textLabel.textColor = _textColor;
    
    cell.textLabel.text = themeKey;
    cell.backgroundColor = [UIColor clearColor];
    cell.imageView.image = iconImage;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //添加钩
    if (_selectRow == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

//表视图点击单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //改变选中索引
    _selectRow = indexPath.row;
    //改变当前主题
    NSString *themeName = _themeDic.allKeys[indexPath.row];
    ThemeManager *manager = [ThemeManager sharedManager];
    manager.themeName = themeName;
    //改变字体颜色
    _textColor = [[ThemeManager sharedManager] themeColorWithColorName:@"More_Item_Text_color"];
    //刷新表视图
    [self.tableView reloadData];

}

@end






